package com.gnetek.netty.filter;

import cn.hutool.core.util.StrUtil;
import javafx.scene.control.TextFormatter;

import java.util.function.UnaryOperator;

/**
 * @Description 长度过滤器
 * @Author Darren Huang
 * @Date 2022-11-22 15:07
 */
public class LengthTextFilter implements UnaryOperator<TextFormatter.Change> {
    protected int maxLength=5;
    public LengthTextFilter(){};
    public LengthTextFilter(int maxLength){
        this.maxLength = maxLength;
    }
    @Override
    public TextFormatter.Change apply(TextFormatter.Change change) {
        if(change!=null){
            String text = change.getControlNewText();
            if(StrUtil.isBlank(text) || text.length()<=maxLength){
                return change;
            }
        }
        return null;
    }
}
